//
//  PieOperationCell.m
//  MonitoringNetwork
//
//  Created by admin on 2018/1/30.
//  Copyright © 2018年 wtmcxlm. All rights reserved.
//

#import "PieOperationCell.h"
#import "PercentModel.h"
#import "PieChartView.h"
#import "NSString+StringSize.h"


@interface PieOperationCell()
@property (nonatomic,strong)PieChartView *pieView;
@property (nonatomic,strong)UILabel *titleLabel;
@property (nonatomic,strong)UILabel *titleLabel2;
@property (nonatomic,strong)UILabel *percentLabel;
@end
@implementation PieOperationCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setUpUI];
    }
    return self;
}

- (void)setUpUI
{
    CGPoint layerPoint;
    CGFloat radiu = 0;
    if (iPhone6p) {
        layerPoint = CGPointMake(100, 150-110/3);
        radiu = 69.0f;
    }else if (iPhone6){
        layerPoint = CGPointMake(90, 150-110/3);
        radiu = 60.0f;
    }else if (iPhoneX){
        layerPoint = CGPointMake(90, 150-110/3);
        radiu = 60.0f;
    }else if(iPhone5){
        layerPoint = CGPointMake(80, 150-110/3);
        radiu = 50.0f;
    }else{
        layerPoint = CGPointMake(100, 150-110/3);
        radiu = 69.0f;
    }
    
    //    if (iPhone6p) {
    //        layerPoint = CGPointMake(110, 150-110/3);
    //    }else{
    //        layerPoint = CGPointMake(80, 150-110/3);
    //    }
    UIView *backView = [UIView new];
    backView.backgroundColor = [UIColor colorWithRed:0.97f green:0.97f blue:0.97f alpha:1.00f];
    [self addSubview:backView];
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(self);
        make.height.mas_equalTo(11);
    }];
    _titleLabel2 = [UILabel LabelWithFont:TitleLagerFontSize andColor:TitleColor andTextAlignment:left andString:@"运营状态"];
    [self addSubview:_titleLabel2];
    [_titleLabel2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(33);
        make.bottom.mas_equalTo(self.mas_top).offset(46);
    }];
    _pieView = [PieChartView new];
    [self addSubview:_pieView];
    Kweaself
    _pieView.layerModel = ^(PercentModel *model) {
        weakSelf.titleLabel.text = model.title;
        weakSelf.percentLabel.text = model.percentStr;
    };
    [_pieView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self);
        make.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self);
        make.top.mas_equalTo(self).offset(46);
    }];
    
    UIBezierPath *roundPath = [UIBezierPath bezierPath];
    [roundPath moveToPoint:layerPoint];
    
    [roundPath addArcWithCenter:CGPointMake(layerPoint.x, 150-110/3+46) radius:radiu-20 startAngle:0 endAngle:M_PI*2 clockwise:YES];
    CAShapeLayer *roundLayder = [CAShapeLayer layer];
    roundLayder.fillColor = [UIColor whiteColor].CGColor;
    roundLayder.path = roundPath.CGPath;
    [self.layer addSublayer:roundLayder];
    
    _titleLabel = [UILabel LabelWithFont:NormalFontSize andColor:TitleColor andTextAlignment:center andString:@""];
    _titleLabel.numberOfLines = 0;
    [self addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_pieView.mas_top).offset(150-110/3-15);
        make.centerX.mas_equalTo(_pieView.mas_left).offset(layerPoint.x);
    }];
    
    _percentLabel = [UILabel LabelWithFont:NormalFontSize andColor:TitleColor andTextAlignment:center andString:@""];
    [self addSubview:_percentLabel];
    [_percentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_pieView.mas_top).offset(160-110/3+5);
        make.centerX.mas_equalTo(_pieView.mas_left).offset(layerPoint.x);
    }];
    
    
}
- (void)laodUIWithDataArr:(NSArray *)arr
{
    
    CGPoint layerPoint;
    CGFloat radiu = 0;
    if (iPhone6p) {
        layerPoint = CGPointMake(100, 150-110/3);
        radiu = 69.0f;
    }else if (iPhone6){
        layerPoint = CGPointMake(90, 150-110/3);
        radiu = 60.0f;
    }else if (iPhoneX){
        layerPoint = CGPointMake(90, 150-110/3);
        radiu = 60.0f;
    }else if(iPhone5){
        layerPoint = CGPointMake(80, 150-110/3);
        radiu = 50.0f;
    }else{
        layerPoint = CGPointMake(100, 150-110/3);
        radiu = 69.0f;
    }
    
    if (arr.count == 0) {
        UILabel *centerLabel = [UILabel LabelWithFont:BigFontSize andColor:TitleColor andTextAlignment:center andString:@"无统计数据"];
        [self addSubview:centerLabel];
        [centerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.centerY.mas_equalTo(self);
        }];
        return;
    }
    int total = 0;
    for (int i=0; i<arr.count; i++) {
        NSDictionary *dict = arr[i];
        total += [dict[@"value"] intValue];
    }
    NSMutableArray *percentArr = [NSMutableArray array];
    NSString *str = @"%";
    NSArray *colorArr = @[ColorOne,ColorTwo,ColorThree,ColorFour,ColorFive];
    
    for (int i=0; i<arr.count ; i++) {
        NSDictionary *dict = arr[i];
        PercentModel *model = [PercentModel new];
        if ([dict[@"value"] integerValue] == 0) {
            model.percent = 0;
            model.percentStr = [NSString stringWithFormat:@"%.2lf%@",0.00,str];
        }else{
            model.percent = [dict[@"value"] doubleValue]/total*1.0f;
            model.percentStr = [NSString stringWithFormat:@"%.2lf%@",[dict[@"value"] doubleValue]/total*100,str];
        }
        model.value = [dict[@"value"] intValue];
        model.title = [NSString stringWithFormat:@"%@",dict[@"key"]];
        [percentArr addObject:model];
    }
    
    NSComparator sortBlock = ^(id obj1,id obj2) {
        return -[@(((PercentModel *)obj1).value) compare: @(((PercentModel *)obj2).value)];
    };
    
    [percentArr sortUsingComparator:sortBlock];
    
    NSMutableArray *percentSortArr = [NSMutableArray array];
    CGFloat otherPercent = 0;
    int otherNum = 0;
    if (percentArr.count<=5) {
        for (int i=0; i<arr.count; i++) {
            PercentModel *model = percentArr[i];
            [percentSortArr addObject:model];
        }
    }else{
        for (int i=0; i<percentArr.count; i++) {
            PercentModel *model = percentArr[i];
            if (i<3 && ![model.title isEqualToString:@"其他"]) {
                [percentSortArr addObject:model];
            }else if (i<3 && [model.title isEqualToString:@"其他"]){
                otherPercent += model.percent;
                otherNum += model.value;
            }else {
                if (percentSortArr.count<4) {
                    [percentSortArr addObject:model];
                }else{
                    otherPercent += model.percent;
                    otherNum += model.value;
                    if (i == percentArr.count-1) {
                        PercentModel *tempmodel = [PercentModel new];
                        tempmodel.title = @"其他";
                        tempmodel.percent = otherPercent;
                        tempmodel.value = otherNum;
                        tempmodel.percentStr = [NSString stringWithFormat:@"%.2lf%@",otherPercent*100.f,str];
                        [percentSortArr addObject:tempmodel];
                    }
                }
            }
        }
    }
    
    [percentSortArr sortUsingComparator:sortBlock];
    
    PercentModel *tempmodel = percentSortArr[0];
    _titleLabel.text = tempmodel.title;
    _percentLabel.text = tempmodel.percentStr;
    
    
    [self.pieView loadPieChartWithArr:percentSortArr.copy adnCenter:layerPoint andColorArr:colorArr];
    [self loadLabelChartWithArr:percentSortArr.copy andColorArr:colorArr];
    
    [_pieView stroke];
}

//不换行
- (void)loadLabelChartWithArr:(NSArray *)arr andColorArr:(NSArray *)colorArr
{
    CGPoint layerPoint;
    CGFloat radiu = 0;
    if (iPhone6p) {
        layerPoint = CGPointMake(100, 150-110/3);
        radiu = 69.0f;
    }else if (iPhone6){
        layerPoint = CGPointMake(90, 150-110/3);
        radiu = 60.0f;
    }else if (iPhoneX){
        layerPoint = CGPointMake(90, 150-110/3);
        radiu = 60.0f;
    }else if(iPhone5){
        layerPoint = CGPointMake(80, 150-110/3);
        radiu = 50.0f;
    }else{
        layerPoint = CGPointMake(100, 150-110/3);
        radiu = 69.0f;
    }
    
    
    NSString *tempTitle = @"";
    for (int i=0; i<arr.count; i++) {
        PercentModel *model = arr[i];
        if (tempTitle.length<model.title.length) {
            tempTitle = model.title;
        }
    }
    //    UILabel *tempLabel = [UILabel new];
    //    tempLabel.adjustsFontSizeToFitWidth = YES;
    //    tempLabel.text = tempTitle;
    //    tempLabel.numberOfLines = 1;
    //    tempLabel.frame = CGRectMake(0, 0, kScreenWidth-100-69-21-50, 50);
    //    [self addSubview:tempLabel];
    //
    //    UIFont *titleFont = tempLabel.font;
    
    //    tempLabel.hidden = YES;
    
    
    
    
    CGFloat y = (299-32*2)*0.5f + 11;
    for (int i=0; i<arr.count; i++) {
        @autoreleasepool {
            PercentModel *model = arr[i];
            UIView * view = [[UIView alloc] init];
            if (iPhone6p) {
                view.frame = CGRectMake(100+69+21+20, y + 32*i, 15.0f, 15.0f);
            }else if (iPhone6){
                view.frame = CGRectMake(90+60+21+20, y + 32*i , 15.0f, 15.0f);
            }else if (iPhoneX){
                view.frame = CGRectMake(90+60+21+20, y + 32*i, 15.0f, 15.0f);
            }else if(iPhone5){
                view.frame = CGRectMake(80+50+21+20, y + 32*i , 15.0f, 15.0f);
            }else{
                view.frame = CGRectMake(100+69+21+20, y + 32*i , 15.0f, 15.0f);
            }
            UIColor *colorff = colorArr[i%5];
            view.backgroundColor = colorff;
            [self addSubview:view];
            UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:view.bounds cornerRadius:2.0f];
            CAShapeLayer *pathLayer = [CAShapeLayer layer];
            pathLayer.lineWidth = 0.5f;
            pathLayer.strokeColor = colorff.CGColor;
            pathLayer.path = path.CGPath;
            view.layer.mask = pathLayer;
            
            UILabel *titleLabe = [UILabel LabelWithFont:NormalFontSize andColor:TitleColor andTextAlignment:left andString:model.title];
            //            titleLabe.adjustsFontSizeToFitWidth = YES;
            titleLabe.numberOfLines= 1;
            [self addSubview:titleLabe];
            [titleLabe mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(view.mas_right).offset(kWidthOfPt(50));
                make.centerY.mas_equalTo(view.mas_centerY);
                make.width.mas_equalTo(kScreenWidth -(100+69+21+15));
            }];
            
            UILabel *numLabel = [UILabel LabelWithFont:NormalFontSize andColor:TitleColor andTextAlignment:left andString:[NSString stringWithFormat:@"%d",model.value]];
            [self addSubview:numLabel];
            
            [numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(60);
                make.right.mas_equalTo(self.mas_right).offset(-kWidthOfPt(50));
                make.centerY.mas_equalTo(view.mas_centerY);
            }];
            
        }
        
    }
    //        @autoreleasepool {
    //            PercentModel *model = arr[i];
    //            UIView * view = [[UIView alloc] init];
    //            if (iPhone6p) {
    //                view.frame = CGRectMake(kScreenWidth-kWidthOfPt(200)-LabelWide-numLabelWide-21, y + labelHeight , 21.0f, 21.0f);
    //            }else if (iPhone4||iPhone4){
    //                view.frame = CGRectMake(kScreenWidth-kWidthOfPt(100)-LabelWide-numLabelWide-21, y + labelHeight , 21.0f, 21.0f);
    //            }else{
    //                view.frame = CGRectMake(kScreenWidth-kWidthOfPt(150)-LabelWide-numLabelWide-21, y + labelHeight , 21.0f, 21.0f);
    //            }
    //            UIColor *colorff = colorArr[i%5];
    //            view.backgroundColor = colorff;
    //            [self addSubview:view];
    //            UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:view.bounds cornerRadius:6.0f];
    //            CAShapeLayer *pathLayer = [CAShapeLayer layer];
    //            pathLayer.lineWidth = 2;
    //            pathLayer.strokeColor = colorff.CGColor;
    //            pathLayer.path = path.CGPath;
    //            view.layer.mask = pathLayer;
    //
    //            UILabel *titleLabe = [UILabel LabelWithFont:NormalFontSize andColor:TitleColor andTextAlignment:left andString:model.title];
    //            titleLabe.numberOfLines= 0;
    //            labelSize = [model.title sizeWithLabelWidth:fontWidth font:[UIFont systemFontOfSize:NormalFontSize]];
    //            if (labelSize.height>32) {
    //                labelHeight += (labelSize.height +11);
    //            }else{
    //                labelHeight += 32.0f;
    //            }
    //
    //            [self addSubview:titleLabe];
    //            [titleLabe mas_makeConstraints:^(MASConstraintMaker *make) {
    //                make.left.mas_equalTo(view.mas_right).offset(kWidthOfPt(20));
    //                make.top.mas_equalTo(view.mas_top);
    //                make.width.mas_equalTo(LabelWide);
    //            }];
    //
    //            UILabel *numLabel = [UILabel LabelWithFont:NormalFontSize andColor:TitleColor andTextAlignment:left andString:[NSString stringWithFormat:@"%d",model.value]];
    //            [self addSubview:numLabel];
    //            CGFloat width = 0;
    //            if (iPhone6p) {
    //                width = 100;
    //            }else{
    //                width = 33;
    //            }
    //            [numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    //                make.right.mas_equalTo(self.mas_right).offset(-kWidthOfPt(width));
    //                make.width.mas_equalTo(numLabelWide);
    //                make.top.mas_equalTo(view.mas_top);
    //            }];
    //        }
    //    }
}


//换行的方法
//- (void)loadLabelChartWithArr:(NSArray *)arr andColorArr:(NSArray *)colorArr
//{
//    CGFloat labelViewHeight =  0;
//    CGFloat y = 0;
//    CGSize labelSize;
//    CGFloat labelHeight = 0;
//    CGFloat LabelWide = 0;
//    CGFloat numLabelWide = 0;
//    CGFloat fontWidth = 0;
//    if (IS_SmallScreen) {
//        fontWidth = 60;
//    }else{
//        fontWidth = 80;
//    }
//    for (int i=0; i<arr.count; i++) {
//        PercentModel *model = arr[i];
//        labelSize = [model.title sizeWithLabelWidth:fontWidth font:[UIFont systemFontOfSize:NormalFontSize]];
//        if (labelSize.height>32) {
//            labelViewHeight = labelViewHeight + (labelSize.height+11);
//        }else{
//            labelViewHeight = labelViewHeight + 32;
//        }
//
//        CGSize numLabelSize = [[NSString stringWithFormat:@"%d",model.value] sizeWithLabelWidth:60 font:[UIFont systemFontOfSize:NormalFontSize]];
//
//        if (labelSize.width>LabelWide) {
//            LabelWide = labelSize.width;
//        }
//
//        if (numLabelSize.width>numLabelWide) {
//            numLabelWide = numLabelSize.width;
//        }
//    }
//
//    y = (299-labelViewHeight)*0.5f + 11;
//    for (int i=0; i<arr.count; i++) {
//        @autoreleasepool {
//            PercentModel *model = arr[i];
//            UIView * view = [[UIView alloc] init];
//            if (iPhone6p) {
//                view.frame = CGRectMake(kScreenWidth-kWidthOfPt(200)-LabelWide-numLabelWide-21, y + labelHeight , 21.0f, 21.0f);
//            }else if (iPhone4||iPhone4){
//                view.frame = CGRectMake(kScreenWidth-kWidthOfPt(100)-LabelWide-numLabelWide-21, y + labelHeight , 21.0f, 21.0f);
//            }else{
//                view.frame = CGRectMake(kScreenWidth-kWidthOfPt(150)-LabelWide-numLabelWide-21, y + labelHeight , 21.0f, 21.0f);
//            }
//            UIColor *colorff = colorArr[i%5];
//            view.backgroundColor = colorff;
//            [self addSubview:view];
//            UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:view.bounds cornerRadius:6.0f];
//            CAShapeLayer *pathLayer = [CAShapeLayer layer];
//            pathLayer.lineWidth = 2;
//            pathLayer.strokeColor = colorff.CGColor;
//            pathLayer.path = path.CGPath;
//            view.layer.mask = pathLayer;
//
//            UILabel *titleLabe = [UILabel LabelWithFont:NormalFontSize andColor:TitleColor andTextAlignment:left andString:model.title];
//            titleLabe.numberOfLines= 0;
//            labelSize = [model.title sizeWithLabelWidth:fontWidth font:[UIFont systemFontOfSize:NormalFontSize]];
//            if (labelSize.height>32) {
//                labelHeight += (labelSize.height +11);
//            }else{
//                labelHeight += 32.0f;
//            }
//
//            [self addSubview:titleLabe];
//            [titleLabe mas_makeConstraints:^(MASConstraintMaker *make) {
//                make.left.mas_equalTo(view.mas_right).offset(kWidthOfPt(20));
//                make.top.mas_equalTo(view.mas_top);
//                make.width.mas_equalTo(LabelWide);
//            }];
//
//            UILabel *numLabel = [UILabel LabelWithFont:NormalFontSize andColor:TitleColor andTextAlignment:left andString:[NSString stringWithFormat:@"%d",model.value]];
//            [self addSubview:numLabel];
//            CGFloat width = 0;
//            if (iPhone6p) {
//                width = 100;
//            }else{
//                width = 33;
//            }
//            [numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//                make.right.mas_equalTo(self.mas_right).offset(-kWidthOfPt(width));
//                make.width.mas_equalTo(numLabelWide);
//                make.top.mas_equalTo(view.mas_top);
//            }];
//        }
//    }
//}
- (void)layoutSubviews
{
    [super layoutSubviews];
    _titleLabel2.text = _title;
}
@end
